package com.github.littleantfly.dictionary.service.impl;


import com.github.littleantfly.dictionary.cache.DictCache;
import com.github.littleantfly.dictionary.module.DictModel;
import com.github.littleantfly.dictionary.service.DictService;

import java.util.Optional;

/**
 * 默认的字典查询服务, 初始化时会加载数据到缓存中
 * @author xiezhenglong
 */
public class DefaultDictServiceImpl implements DictService {

    private final DictCache dictCache;

    public DefaultDictServiceImpl(DictCache dictCache) {
        this.dictCache = dictCache;
    }


    @Override
    public Optional<DictModel> getDictData(String key, Object fieldValue) {
        if(key ==null || key.isEmpty()) {
            throw new IllegalArgumentException("key 不能为空");
        }
        if (fieldValue == null) {
            return Optional.empty();
        }
        return dictCache.get(String.format("%s-%s", key, fieldValue));
    }


}
